package com.xzkydz.sensor.utils;

/**
 * 数据转换工具
 * <p>
 * Android 数据在内存中，高字节在前、低字节在后；
 * 举例说明
 * byte[] data = {0xff,0x56};   0xff56
 * 实际二进制为：0000 0000  0000 0000  1111 1111  0101 0110
 * 转换为十进制：2^15 + 2^14 + ... + 2^8 + 2^6 + 2^4 + 2^2 + 2^1
 */
public class CalculationUtils {

    /**
     * 1字节转为无符号整数
     *
     * @param a
     * @return
     */
    public static int oneByteToIntNoSign(byte a) {
        return Byte.toUnsignedInt(a);
    }

    /**
     * 双字节转无符号int
     *
     * @return
     */
    public static int twoByteToIntNoSign(byte a, byte b) {
        return Byte.toUnsignedInt(a) * 256 + Byte.toUnsignedInt(b);
    }

    /**
     * 无符号int转双字节
     *
     * @return
     */
    public static byte[] intNoSignToTwoByte(int a) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) ((a >> 8) & 0xff);
        bytes[1] = (byte) (a & 0xff);
        return bytes;
    }

    /**
     * 2字节转有符号short
     *
     * @return
     */
    public static short twoBytesToIntSign(byte a, byte b) {
        return (short) ((a<< 8 & 0xffff) | (b & 0xff));
    }

    /**
     * 两字节转int  有符号位
     * 功率箱 P Q  解析
     *
     * @param a ： 第一字节
     * @param b ： 第二字节
     * @return
     */
    public static int twoBytesToIntHave(byte a, byte b) {
        int l = b;
        l &= 0xff;
        l |= ((long) a << 8);
        l &= 0xffff;
        l = l << 16;
        l = l / (int) Math.pow(2, 16);
        return l;
    }

    /**
     * 3字节转无符号int
     *
     * @return
     */
    public static int threeBytesToInt(byte a, byte b, byte c) {
        return Byte.toUnsignedInt(a) * 65536 + Byte.toUnsignedInt(b) * 256 + Byte.toUnsignedInt(c);
    }


    /**
     * 4字节转有符号int
     *
     * @return
     */
    public static int fourBytesToInt(byte a, byte b, byte c, byte d) {
        return (a & 0xffffffff) << 24 | (b & 0xffffffff) << 16 | (c & 0xffffffff) << 8 | d;
    }

    /**
     * 4字节转无符号int
     *
     * @return
     */
    public static long fourBytesToIntNoSign(byte a, byte b, byte c, byte d) {
        return Byte.toUnsignedInt(a) * 16777216L + Byte.toUnsignedInt(b) * 65536L + Byte.toUnsignedInt(c) * 256L + Byte.toUnsignedInt(d);
    }

    /**
     * 字节转换为浮点
     *
     * @param b     字节（至少4个字节）
     * @param index 开始位置
     * @return
     */
    public static float fourBytesToFloat(byte[] b, int index) {
        byte[] by = new byte[4];
        by[0] = b[index + 3];
        by[1] = b[index + 2];
        by[2] = b[index + 1];
        by[3] = b[index];
        int l;
        l = by[0];
        l &= 0xff;
        l |= ((long) by[1] << 8);
        l &= 0xffff;
        l |= ((long) by[2] << 16);
        l &= 0xffffff;
        l |= ((long) by[3] << 24);
        return Float.intBitsToFloat(l);
    }


    /**
     * 4字节转换为有符号float
     */
    public static String twoBytesToHexStr(byte a, byte b) {
        return byte2Hex(a) + byte2Hex(b);
    }

    /**
     * Hex字符串转int
     *
     * @param inHex 十六进制字符串
     * @return
     */
    public static int hexToInt(String inHex) {
        return Integer.parseInt(inHex, 16);
    }


    /**
     * 判断奇数或偶数，位运算，最后一位是1则为奇数，为0是偶数
     *
     * @param num
     * @return
     */
    static public int isOdd(int num) {
        return num & 0x1;
    }


    /**
     * Hex字符串转byte
     *
     * @param inHex
     * @return
     */
    static public byte hexToByte(String inHex) {
        return (byte) Integer.parseInt(inHex, 16);
    }

    /**
     * 1字节转2个Hex字符
     *
     * @param inByte
     * @return
     */
    static public String byte2Hex(Byte inByte) {
        return String.format("%02x", inByte).toUpperCase();
    }

    /**
     * 字节数组转转hex字符串
     *
     * @param inBytArr
     * @return
     */
    public static String byteArrToHex(byte[] inBytArr) {
        StringBuilder strBuilder = new StringBuilder();
        int j = inBytArr.length;
        for (int i = 0; i < j; i++) {
            strBuilder.append(byte2Hex(inBytArr[i]));
            strBuilder.append(" ");
        }
        return strBuilder.toString();
    }

    /**
     * 字节数组转转hex字符串，可选长度
     *
     * @param inBytArr
     * @param offset
     * @param byteCount
     * @return
     */
    public static String byteArrToHex(byte[] inBytArr, int offset, int byteCount) {
        StringBuilder strBuilder = new StringBuilder();
        int j = byteCount;
        for (int i = offset; i < j; i++) {
            strBuilder.append(byte2Hex(inBytArr[i]));
        }
        return strBuilder.toString();
    }

    /**
     * hex字符串转字节数组
     *
     * @param inHex
     * @return
     */
    static public byte[] hexToByteArr(String inHex) {
        int hexlen = inHex.length();
        byte[] result;
        if (isOdd(hexlen) == 1) {//奇数
            hexlen++;
            result = new byte[(hexlen / 2)];
            inHex = "0" + inHex;
        } else {//偶数
            result = new byte[(hexlen / 2)];
        }
        int j = 0;
        for (int i = 0; i < hexlen; i += 2) {
            result[j] = hexToByte(inHex.substring(i, i + 2));
            j++;
        }
        return result;
    }

    public static float arryToFloat(byte[] Array, int Pos) {
        int accum = 0;
        accum = Array[Pos + 0] & 0xFF;
        accum |= (long) (Array[Pos + 1] & 0xFF) << 8;
        accum |= (long) (Array[Pos + 2] & 0xFF) << 16;
        accum |= (long) (Array[Pos + 3] & 0xFF) << 24;
        return Float.intBitsToFloat(accum);
    }
}